package com.test.rabbitmq.simple;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.Connection;
import com.test.rabbitmq.BaseRunnable;
import com.test.rabbitmq.BaseThread;
import com.test.rabbitmq.RabbitMQUtil;

public class Publisher extends BaseRunnable {

    private final static String QUEUE_NAME = "q_test_01";

    @Override
    public void execute() throws Exception {
        // 获取到连接以及mq通道
        Connection connection = RabbitMQUtil.getConn("user", "user");
        // 从连接中创建通道
        Channel channel = connection.createChannel();
        // 声明（创建）队列
        channel.queueDeclare(QUEUE_NAME, false, false, false, null);
        // 消息内容
        for (int i = 0; i < 10; i++) {
            // 消息内容
            String message = String.format("Hello World! Mr %s", i);
            channel.basicPublish("", QUEUE_NAME, null, message.getBytes());
            System.out.println(" [x] Sent '" + message + "'");
            Thread.sleep(10);
        }
        //关闭通道和连接
        channel.close();
        connection.close();
    }
}
